//-----------------------------------------------------------
//
//-----------------------------------------------------------
class ZFGame extends ROTeamGame;

var UnrealTeamInfo ZombieTeam;
var() string  ZombieTeamClassName;
var() string  ZombiePawnClassName;
var() string  ZombieBotControllerClassName;

replication
{
    reliable if(role < ROLE_Authority)
        SpawnZombieBot, SpawnZombiePawn;
}

exec function Zombie()
{
    SpawnZombieBot();
}

function SpawnZombieBot()
{
    local Bot ZombieBot;
    local class<Controller> ZombieBotClass;
    local class<Pawn> ZombiePawnClass;

    ZombieBotClass = class<Controller>( DynamicLoadObject( ZombieBotControllerClassName, class'Class') );
    ZombiePawnClass = class<Pawn>( DynamicLoadObject( ZombiePawnClassName, class'Class') );

    ZombieBot = Bot(Spawn(ZombieBotClass));
    InitializeZombie(ZombieBot,GetZombieTeam(),GetZombieTeam().ChooseBotClass());

    if ( ZombieBot != none )
        SpawnZombiePawn(ZombieBot);
}

function SpawnZombiePawn( Controller aZombie )
{
    local NavigationPoint startSpot;
    local int TeamNum;

    if( bRestartLevel && Level.NetMode!=NM_DedicatedServer && Level.NetMode!=NM_ListenServer )
        return;

    TeamNum = aZombie.PlayerReplicationInfo.TeamID;

    startSpot = FindZombieStart(aZombie,TeamNum);
    if( startSpot == none )
    {
        log("Zombie Start not found!!!");
        return;
    }

    if ( aZombie.PawnClass == none )
        aZombie.PawnClass = class<Pawn>( DynamicLoadObject( ZombiePawnClassName, class'Class') );
    aZombie.Pawn = Spawn(aZombie.PawnClass,,,StartSpot.Location,StartSpot.Rotation);
    if ( aZombie.Pawn == None )
    {
        log("Couldn't spawn zombie of type "$aZombie.PawnClass$" at "$StartSpot);
        aZombie.GotoState('Dead');
        return;
    }
    if ( PlayerController(aZombie) != None )
		PlayerController(aZombie).TimeMargin = -0.1;

    aZombie.Pawn.Anchor = startSpot;
	aZombie.Pawn.LastStartSpot = PlayerStart(startSpot);
	aZombie.Pawn.LastStartTime = Level.TimeSeconds;
    aZombie.PreviousPawnClass = aZombie.Pawn.Class;

    aZombie.Possess(aZombie.Pawn);
    aZombie.PawnClass = aZombie.Pawn.Class;

    aZombie.Pawn.PlayTeleportEffect(true, true);
    aZombie.ClientSetRotation(aZombie.Pawn.Rotation);
    //AddDefaultInventory(aPlayer.Pawn);
    TriggerEvent( StartSpot.Event, StartSpot, aZombie.Pawn);
}

function NavigationPoint FindZombieStart( Controller Player, optional byte InTeam, optional string incomingName )
{
    local NavigationPoint N, BestStart;
    local float BestRating, NewRating;
    local byte Team;

    if ( GameRulesModifiers != None )
    {
        N = GameRulesModifiers.FindPlayerStart(Player,InTeam,incomingName);
        if ( N != None )
            return N;
    }

    Team = InTeam;

    for ( N=Level.NavigationPointList; N!=None; N=N.NextNavigationPoint )
    {
        NewRating = RatePlayerStart(N,Team,Player);
        if ( NewRating > BestRating )
        {
            BestRating = NewRating;
            BestStart = N;
        }
    }

    if ( (BestStart == None) || ((PlayerStart(BestStart) == None) && (Player != None) && Player.bIsPlayer) )
    {
        log("Warning - PATHS NOT DEFINED or NO PLAYERSTART with positive rating");
		BestRating = -100000000;
        ForEach AllActors( class 'NavigationPoint', N )
        {
            NewRating = RatePlayerStart(N,0,Player);
            if ( InventorySpot(N) != None )
				NewRating -= 50;
			NewRating += 20 * FRand();
            if ( NewRating > BestRating )
            {
                BestRating = NewRating;
                BestStart = N;
            }
        }
    }

    return BestStart;
}

function float RateZombieStart(NavigationPoint N, byte Team, Controller Player)
{
    local PlayerStart P;

    P = PlayerStart(N);
    if ( P != None )
    {
        return 10;
    }
    return 0;
}

function UnrealTeamInfo GetZombieTeam()
{
    if ( ZombieTeam != none )
        return ZombieTeam;
    ZombieTeam = Spawn(class<UnrealTeamInfo>(DynamicLoadObject(ZombieTeamClassName,class'Class')));
    ZombieTeam.Initialize(0);
    return ZombieTeam;
}

function InitializeZombie(Bot NewZombie, UnrealTeamInfo BotTeam, RosterEntry Chosen)
{
    NewZombie.InitializeSkill(AdjustedDifficulty);
 	//Chosen.InitBot(NewZombie);
    ZombieTeam.AddToTeam(NewZombie);
	ChangeName(NewZombie, "Zombie", false);
	//BotTeam.SetBotOrders(NewZombie,Chosen);
}

event InitGame( string Options, out string Error )
{
    super.InitGame(Options,Error);

    class'ROInventory.PTRDFire'.default.ProjectileClass=class'ZFCore.ZF_PTRDBullet';
}

defaultproperties
{
    //______________________________________
    // Basic game definition ---------------
    MapPrefix="ZF"
    Acronym="ZF"
    BeaconName="ZF"
    GameName="Zombie Front Game"
    Description="Pwn some zombies with your friends or pwn your friends!"
    //DefaultPlayerClassName="ZFCore.ZFPawn"
    ZombieTeamClassName="xGame.xDMRoster"
    ZombiePawnClassName="ZFCore.ZFPawn"
    ZombieBotControllerClassName="xGame.xBot"//"ZFCore.ZFBot"
}
